package com.sgdh.project.terminal.program.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sgdh.common.exception.BusinessException;
import com.sgdh.common.utils.security.ShiroUtils;
import com.sgdh.common.utils.text.Convert;
import com.sgdh.project.system.post.domain.Post;
import com.sgdh.project.terminal.program.domain.Program;
import com.sgdh.project.terminal.program.mapper.ProgramMapper;
import com.sgdh.project.terminal.program.service.IProgramService;

/**
 * 用户 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class ProgramServiceImpl implements IProgramService
{
    private static final Logger log = LoggerFactory.getLogger(ProgramServiceImpl.class);

    @Autowired
    private ProgramMapper programMapper;


    /**
     * 根据条件分页查询项目列表
     * 
     * @param program 项目信息
     * @return 项目信息集合信息
     */
	@Override
	public List<Program> selectProgramList(Program program) {
		if(!ShiroUtils.getSysUser().isAdmin()) {
			program.getParams().put("userId", ShiroUtils.getUserId());
		}
        return programMapper.selectProgramList(program);
	}

	/**
     * 新增项目
     * 
     * @param program 项目信息
     * @return 操作结果
     */
	@Override
	public int insertProgram(Program program) {
		int result = 0;
		try {
			program.setCreateBy(ShiroUtils.getSysUser().getUserId().toString());
			program.setCreateTime(new Date());
			result = programMapper.insertProgram(program);
		} catch (Exception e) {
			result = 0;
			e.printStackTrace();
		}
		return result;
	}

	/**
     * 根据项目Id获取项目明细
     * 
     * @param id 项目id
     * @return Program  项目详情
     */
	@Override
	public Program selectProgramById(int id) {
		// TODO Auto-generated method stub
		return programMapper.selectProgramById(id);
	}

	/**
     * 修改保存项目信息
     * 
     * @param program 项目信息
     * @return 修改操作结果
     */
	@Override
	public int updateProgram(Program program) {
		int result = 0;
		try {
			program.setUpdateBy(ShiroUtils.getSysUser().getUserId().toString());
			program.setUpdateTime(new Date());
			result = programMapper.updateProgram(program);
		} catch (Exception e) {
			result = 0;
			e.printStackTrace();
		}
		return result;
	}

	/**
     * 删除项目
     * 
     * @param ids 项目id集合
     * @return 操作结果
     */
	@Override
	public int deleteProgramByIds(String ids) {
		int result = 0;
		try {
			Integer[] intArray = Convert.toIntArray(ids);
	        result = programMapper.deleteProgramByIds(intArray);
		} catch (Exception e) {
			result = 0;
			e.printStackTrace();
		}
		return result;
	}

    
}
